package com.hsq.commons.util;

/**
 *
 * @since 2001-12-18
 * @author lijun
 * @version 1.0
 * @Copyright ? 2009 LinkCm Co. Ltd. All right reserved.
 */

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

public class Test {
	public static String LOGGER_NAME = "velexample";

	public Test() throws ResourceNotFoundException, ParseErrorException,
			Exception {
		VelocityEngine ve = new VelocityEngine();
		Properties properties = new Properties();
		String path = this.getClass().getResource("/").toString().replaceAll(
				"^file:/", "").replaceAll("WEB-INF/classes/", "");
		System.out.println(path);
		properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
		try {
			ve.init(properties);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Template t = ve.getTemplate("mytemplate.vm");
		StringWriter writer = new StringWriter();
		VelocityContext context = new VelocityContext();
		List list = new ArrayList();
		for (int i = 0; i < 10; i++) {
			list.add(i);
		}
		context.put("name", list);
		t.merge(context, writer);
		System.out.println(writer.toString());

	}

	public static void main(String args[]) throws Exception {
		new Test();

	}
}
